package com.mvc.adaptor;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mvc.annotation.Adaptor;
import com.mvc.argumentResolver.ArgResolverInterface;

/**
 * mvc组件之适配器
 * 
 * @author ZGame
 *
 */
@Adaptor
public class DefaultAdaptor implements HandlerAdapter {
	public DefaultAdaptor() {
	}

	@Override
	public Object[] hand(HttpServletRequest request, HttpServletResponse response, Method method,
			Map<String, ArgResolverInterface> resolverMap) {
		Parameter[] prameters = method.getParameters();
		Object[] args = new Object[prameters.length];
		for (int i = 0; i < args.length; i++) {
			Object obj = null;
			Parameter parameter = prameters[i];
			ArgResolverInterface resolver = resolverMap.get(parameter.getType().getSimpleName());
			if (resolver != null) {
				obj = resolver.argumentResolver(request, response, parameter);
			}
			args[i] = obj;
		}
		return args;
	}

}
